package cn.cboard.gateway;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;


/**
 * @author 
 *
 * 
 * Converts a result set into a Map - simple converter interface 
 */
public class MapConverter implements ResultSetConverter {

	/**
	 * Given a ResultSet return a strongly typed object representing
	 * one row
	 */
	public Object toObject(ResultSet rs) throws DAOException {
        try {
		Map data = new HashMap();
		if (rs == null)
			return data;

		ResultSetMetaData meta = rs.getMetaData();

		int noCols = meta.getColumnCount();
		for (int i = 1; i <= noCols; ++i) {
			String columnName = meta.getColumnName(i);
			Object columnValue = rs.getObject(i);

			// add to map
			data.put(columnName, columnValue);

		} //rof
		return data;
        }catch (SQLException e) {
            throw new DAOException(e);
        }

	}
}
